package lib.streaming;

import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

import static java.util.stream.Collectors.partitioningBy;

/**
 * Created by apple on 2018/7/1.
 * 质数 别名素数 相关 合数
 */
public class PartitionPrime {

    /**
     *  Partitioning numbers inot prime and nonprime
     */
    public boolean isPrime(int candidate) {
        return IntStream.range(2, candidate).noneMatch(i -> candidate % i == 0);
    }

    public boolean isPrime2(int candidate) {
        int candidateRoot = (int) Math.sqrt((double) candidate);
        return IntStream.rangeClosed(2, candidateRoot).noneMatch(i -> candidate % i == 0);
    }

    /**
     * 将n个数分成是否为质数
     * @param n
     * @return
     */
    public Map<Boolean, List<Integer>> partiionPrimes(int n) {
        return IntStream.rangeClosed(2, n).boxed().collect(partitioningBy(candidate -> isPrime(candidate)));
    }
}
